package com.wf.netty.base;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * 客户端
 *
 * @author wf
 * @since 2022/4/19
 */
public class MyEchoClient {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new MyClientOutHandler());
            ChannelFuture f = bootstrap.connect(new InetSocketAddress(8888)).sync();
            f.sync();
            Channel channel = f.channel();
            ByteBuf buffer = channel.alloc().buffer();
            buffer.writeInt(1);
            channel.writeAndFlush(buffer);
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
}
